# -*- coding: utf-8 -*-
# Project : flacon
# File : tracklist.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


from settings import *
from flaconglobal import *

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from errors import *


class AudioFile(QObject):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        QObject.__init__(self)

        self._infoItems = {}

        FileError.checkFile(fileName)
        self._fileName = QFileInfo(fileName).canonicalFilePath()
        self._loadInfo()


   #****************************************************************
   #
   #****************************************************************
    def _loadInfo(self):
        shntool = settings.value("Programs/shntool").toString()
        proc = tryPopen([unicode(shntool), 'info', unicode(self._fileName)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
        res = proc.wait()

        if res != 0:
            raise FileErrorBadAudio(self._fileName)

        for line in proc.stdout.readlines():
            name = QString(line).section(':', 0, 0).toUpper().trimmed()
            value =QString(line).section(':', 1).trimmed()
            self._infoItems[name]=value;


   #****************************************************************
   #
   #****************************************************************
    def _info(self, name):
        try:
            return QString(self._infoItems[QString(name).toUpper()])
        except:
            return ""


   #****************************************************************
   #
   #****************************************************************
    def fileName(self):
        return self._fileName


   #****************************************************************
   #
   #****************************************************************
    def haveEmbededCue(self):
        return False


   #****************************************************************
   #
   #****************************************************************
    def cdQuality(self):
        return self._info("CD quality") == 'yes'


   #****************************************************************
   #
   #****************************************************************
    def sampleRate(self):
        return int(self._info("Samples/sec"))



